var FrontendController = Class.create(MojaUtils.Observable,{
	routing: {
		"Connect" : {
			"click" : "connectToVNC"
		},
		"Disconnect" : {
			"click" : "disconnectFromVNC"
		},
		"Host" : {
			"change" : "setHost"
		},
		"Port" : {
			"change" : "setPort"
		},
		"Password" : {
			"change" : "setPassword"
		},
		"Secureflag" : {
			"change" : "setSecure"
		}
	},
	
	initialize: function(target) {
		MojaUtils.Observable.prototype.initialize.call(this);
		this.box = $(target.controls);
		this.setUpSignals();
		this.routeSignals();
	},
	
	setUpSignals: function() {
		var boxes = this.box.select('[signal]');
		var curBox = null;
		var scope = this;
		var ev = ['click','change','focus','blur']
		for(var i=0;i<boxes.length;i++) {
			curBox = boxes[i];
			if (!curBox.id) {
				throw ("Input fields without id will be ignored.");
				continue;
			}
			curBox.signal = curBox.attributes.signal.value;
			for (var x = 0; x < ev.length; x++) {
				$(curBox.id).observe(ev[x], function(ev){
					var el = Event.element(ev);
					while(el != window && !el.signal) // bubble
						el = el.parentNode;
					scope.fireEvent(el.signal + "_" + ev.type,el);
			
				});
			}
		}
	},
	connectToVNC: function(elem) {
	
			if (!this.host) {
				alert("Please insert a valid host name or IP Address");
				return true;
			}
			if (!this.port || isNaN(this.port)) {
				alert("Please insert a valid port");
				return true;
			}

			$('status').update("Connecting to <br/>"+this.host+":"+this.port);
			$('disconnectBtn').removeClassName('disabled');
			$('disconnectBtn').disabled = false;
			$('connectBtn').addClassName('disabled');
			$('connectBtn').disabled = true;
			this.createVNCInstance();
		
	},
	
	disconnectFromVNC: function(elem) {
		$('disconnectBtn').addClassName('disabled');
		$('disconnectBtn').disabled = true;
		$('connectBtn').removeClassName('disabled');
		$('connectBtn').disabled = false;
		this.vncInstance.disconnect();
	},
	
	setHost: function(elem) {
		this.host = elem.value;
	},
	
	setPort: function(elem) {
		this.port = elem.value;
	},
	
	setPassword: function(elem) {
		this.password = elem.value;
	},
	
	setSecure: function(elem) {
		this.secure = elem.checked;
	},
	
	createVNCInstance: function() {
		this.vncInstance = new VNCClient(this.host,this.port,this.secure || false,"workarea");

		this.vncInstance.socket.registerEvent("onopen",function() {
			$('status').update("Connected");
			$('ctrlBox').style.display = "none";
			$('status').removeClassName('disconnected');
			$('status').addClassName('connected')
		},this,true);
		
		this.vncInstance.socket.registerEvent("onclose",function() {
			$('status').update("Closed");
			$('status').removeClassName('connected');
		//	$('status').toggleClassName('disconnected');
		},this,true);
		
		this.vncInstance.socket.registerEvent("onerror",function() {
			$('status').update("ERROR");
			$('status').removeClassName('disconnected');
			$('status').removeClassName('connected');
		//	$('status').toggleClassName('error')
		},this,true);
		
		this.vncInstance.connect();	
	},
	
	routeSignals: function() {
		for(var signal in this.routing) {
			for(var event in this.routing[signal]) {
				if(typeof this[this.routing[signal][event]] != "function")  {
					MojaUtils.Logger.log("Error in routing: fn "+this.routing[signal][event]+" is no function") 
					continue;
				};
				this.registerEvent(signal+"_"+event,this[this.routing[signal][event]],this,false);
			}
		}
	}
	
});


